package putao.ui
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import flashx.textLayout.elements.BreakElement;
	
	import putao.ui.assets.Assets;
	import putao.ui.events.PWindowEvent;
	
	public class PWindow extends Sprite
	{
		public static const MAX:String = "MAX";
		public static const MIN:String = "MIN";
		public static const NORMAL:String = "NORMAL";
		
		/**
		 * 关闭按钮 
		 */		
		protected var btnClose:PButton = new PButton();
		/**
		 * 最小化按钮 
		 */		
		protected var btnMin:PButton = new PButton();
		/**
		 * 最大化按钮 
		 */		
		protected var btnMax:PButton = new PButton();
		/**
		 * 正常大小按钮 
		 */		
		protected var btnNormal:PButton = new PButton();
		/**
		 * 激活状态的背景
		 */		
		protected var activeBG:DisplayObject;
		/**
		 * 未激活状态的背景 
		 */		
		protected var inactiveBG:DisplayObject;
		/**
		 * 窗口移动热区 
		 */		
		protected var hotSprite:Sprite;
		
		protected var txtTitle:TextField;
		/**
		 * 当前显示的背景 
		 */		
		protected var currentDisObj:DisplayObject;
		/**
		 * 窗口的当前状态，有MAX最大化，MIN最小化，NORMAL普通
		 */		
		protected var _currentState:String
		/**
		 * 记录普通状态下的窗口的矩形 
		 */		
		protected var normalRect:Rectangle = new Rectangle();
		
		public var title:String;
		
		public function PWindow(title:String = "", width:Number = 200, height:Number = 300)
		{
			super();
			
			initDefaultSkin();
			currentDisObj = activeBG;
			this.addChild(currentDisObj);
			this.addChild(btnMin);
			this.addChild(btnMax);
			this.addChild(btnClose);
			txtTitle = new TextField();
			this.title = title;
			txtTitle.text = title;
			txtTitle.selectable = false;
			txtTitle.mouseEnabled = false;
			txtTitle.height = 20;
			txtTitle.autoSize = TextFieldAutoSize.LEFT;
			this.addChild(txtTitle);
			hotSprite = new Sprite();
			hotSprite.graphics.beginFill(0x000000, 0);
			hotSprite.graphics.drawRect(0, 0, 100, 100);
			hotSprite.buttonMode = true;
			this.addChild(hotSprite);
			resize(width, height);
			recordNormalRect();
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddToStage, false, 0, true);
		}
		
		protected function onAddToStage(e:Event):void
		{
			PFocusManager.getInstance(stage).setFocusGroup(this);
			this.removeEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouse, false, 0, true);
			this.addEventListener(MouseEvent.DOUBLE_CLICK, onMouse, false, 0, true);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, onMouse, false, 0, true);
			this.addEventListener(MouseEvent.CLICK, onMouse, false, 0, true);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddAgain, false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemove, false, 0, true);
		}
		
		protected function onRemove(e:Event):void
		{
			PFocusManager.getInstance(stage).deleteFocusGroup(this);
		}
		
		/**
		 *
		 * 再次添加到舞台上
		 *  
		 * @param e
		 * 
		 */		
		protected function onAddAgain(e:Event):void
		{
			PFocusManager.getInstance(stage).setFocusGroup(this);
		}
		
		/**
		 *
		 * 处理各种鼠标事件
		 * 点击关闭按钮后，会把窗口在焦点管理器中删除
		 * 并且焦点组变为舞台
		 * 要重新获得焦点，需要将此窗口添加在舞台上
		 *  
		 * @param e
		 * 
		 */		
		protected function onMouse(e:MouseEvent):void
		{
			switch(e.type)
			{
				case MouseEvent.MOUSE_DOWN:
					if(e.target == hotSprite){
						this.startDrag();
						this.parent.addChild(this);
					}
					break;
				case MouseEvent.MOUSE_UP:
					this.stopDrag();
					break;
				case MouseEvent.CLICK:
					if(e.target == btnClose){
						PFocusManager.getInstance(stage).deleteFocusGroup(this);
						dispatchEvent(new PWindowEvent(PWindowEvent.CLOSE));
					}else if(e.target == btnMax){
						currentState = MAX;
					}else if(e.target == btnNormal){
						currentState = NORMAL;
					}else if(e.target == btnMin){
						currentState = MIN;
					}
					break;
				case MouseEvent.DOUBLE_CLICK:
					if(this.mouseY < 30){
						if(this._currentState == MAX || this._currentState == MIN){
							this.currentState = NORMAL;
						}else{
							this.currentState = NORMAL;
						}
					}
					break;
			}
		}
		
		/**
		 *
		 * 改变窗口的状态
		 *  
		 * @param value
		 * 
		 */		
		protected function set currentState(value:String):void
		{
			if(_currentState == value){
				return;
			}else{
				_currentState = value;
			}
			switch(value)
			{
				case MAX:
					if(this.contains(btnMax)){
						this.removeChild(btnMax);
					}
					this.addChild(btnMin);
					this.addChild(btnNormal);
					this.addChild(btnClose);
					txtTitle.text = title;
					this.x = 0;
					this.y = 0;
					this.resize(this.stage.stageWidth, this.stage.stageHeight);
					dispatchEvent(new PWindowEvent(PWindowEvent.MAX));
					break;
				case NORMAL:
					if(this.contains(btnNormal)){
						this.removeChild(btnNormal);
					}
					this.addChild(btnMin);
					this.addChild(btnMax);
					this.addChild(btnClose);
					txtTitle.text = title;
					this.x = normalRect.x;
					this.y = normalRect.y;
					this.resize(normalRect.width, normalRect.height);
					recordNormalRect();
					dispatchEvent(new PWindowEvent(PWindowEvent.NORMAL));
					break;
				case MIN:
					if(this.contains(btnMax)){
						this.removeChild(btnMax);
					}
					this.removeChild(btnMin);
					this.addChild(btnNormal);
					this.addChild(btnClose);
					if(title.length > 3){
						txtTitle.text = title.slice(0, 2) + "...";
					}
					this.resize(100, 40);
					dispatchEvent(new PWindowEvent(PWindowEvent.MIN));
					break;
			}
		}
		
		/**
		 * 
		 * 记录当前窗口的矩形 
		 * 
		 */		
		protected function recordNormalRect():void
		{
			normalRect.x = this.x;
			normalRect.y = this.y;
			normalRect.width = this.width;
			normalRect.height = this.height;
		}
		
		/**
		 *
		 * 重置窗口大小
		 *  
		 * @param width
		 * @param height
		 * 
		 */		
		public function resize(width:Number, height:Number):void
		{
			activeBG.width = width;
			activeBG.height = height;
			inactiveBG.width = width;
			inactiveBG.height = height;
			var baseW:Number = 20;
			btnClose.x = width - baseW * 1;
			btnMax.x = width - baseW * 2;
			btnNormal.x = width - baseW * 2;
			btnMin.x = width - baseW * 3;
			txtTitle.x = 10;
			txtTitle.y = 3;
			hotSprite.width = width - baseW * 3;
			hotSprite.height = 30;
		}
		
		/**
		 *
		 * 初始化默认皮肤
		 * 
		 */		
		protected function initDefaultSkin():void
		{
			btnClose.setSkin(Assets.getDisplayObject(Assets.FrameCloseDefault), 
				Assets.getDisplayObject(Assets.FrameCloseRollover), 
				Assets.getDisplayObject(Assets.FrameClosePressed), 
				Assets.getDisplayObject(Assets.FrameCloseDisabled));
			btnMin.setSkin(Assets.getDisplayObject(Assets.FrameMinDefault), 
				Assets.getDisplayObject(Assets.FrameMinRollover), 
				Assets.getDisplayObject(Assets.FrameMinPressed), 
				Assets.getDisplayObject(Assets.FrameMinDisabled));
			btnMax.setSkin(Assets.getDisplayObject(Assets.FrameMaxDefault), 
				Assets.getDisplayObject(Assets.FrameMaxRollover), 
				Assets.getDisplayObject(Assets.FrameMaxPressed), 
				Assets.getDisplayObject(Assets.FrameMaxDisabled));
			btnNormal.setSkin(Assets.getDisplayObject(Assets.FrameNormalDefault), 
				Assets.getDisplayObject(Assets.FrameNormalRollover), 
				Assets.getDisplayObject(Assets.FrameNormalPressed), 
				Assets.getDisplayObject(Assets.FrameNormalDisabled));
			activeBG = Assets.getDisplayObject(Assets.FrameActiveBG);
			inactiveBG = Assets.getDisplayObject(Assets.FrameInactiveBG);
		}
	}
}